from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys
import os
import PyQt5.QtChart as chart

import load

class comboPair:
    def __init__(self,x,rootdir,win):
        self.rootdir=rootdir
        self.charView=win.charView
        self.combo2 = QComboBox(win)
        self.combo2.move(x+150, 500)
        self.combo2.activated[str].connect(self.addLine)

        self.combo1 = QComboBox(win)
        self.combo1.move(x, 500)
        # 遍历目录初始化combo1
        list = os.listdir(rootdir)  # 列出文件夹下所有的目录与文件
        for i in range(0, len(list)):
            path = os.path.join(rootdir, list[i])
            if os.path.isfile(path):
                self.combo1.addItem(list[i])  # 文件名
        self.combo1.activated[str].connect(self.onActivatLoad)

    def onActivatLoad(self, text):
        path=self.rootdir+text
        self.nowData=text
        self.loadCombo(path)

    def loadCombo(self,filename):
        self.data,allCol=load.load(filename)
        self.combo2.clear()
        for i in allCol:
            self.combo2.addItem(i)

    def addLine(self, yText):
        x = load.getCol('X',self.data)
        y = load.getCol(yText,self.data)
        line = load.genLine(x, y, yText+'('+self.nowData+')')
        self.charView.chart().addSeries(line)  # 添加折线
        self.charView.chart().createDefaultAxes()  # 使用默认坐标系


class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initChart() # 必须先初始化表再创建comboPair
        self.initUI()
        self.setWindowFlags(Qt.WindowMinimizeButtonHint | Qt.WindowCloseButtonHint)
        self.resize(800, 600)
        self.setWindowTitle('test')
        self.show()


    def initChart(self):
        self.charView = chart.QChartView(self)
        self.charView.setGeometry(0, 0, 800, 500)  # 设置charView位置、大小
        # self.charView.chart().createDefaultAxes() #使用默认坐标系
        self.charView.chart().setTitleBrush(QBrush(Qt.cyan))  # 设置标题笔刷
        # self.charView.chart().setTitle("双折线")  # 设置标题
        self.charView.show()  # 显示charView

    def resetChart(self):
        self.charView.deleteLater()
        self.initChart()

    def initUI(self):
        rootdir = './data/'
        # self.lbl = QLabel("Ubuntu", self)
        # self.lbl.adjustSize()
        btn_1 = QPushButton(self)
        btn_1.setText('清空')
        btn_1.clicked.connect(self.resetChart)
        btn_1.move(650, 530)

        self.c1=comboPair(15,rootdir,self)
        self.c2=comboPair(265, rootdir, self)
        self.c3=comboPair(565, rootdir, self)


app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
